package com.med.controller;

import com.med.entity.CustomerMessageDO;
import com.med.entity.DoctorMessageDO;
import com.med.service.IChatService;
import com.med.vo.CustomerMessageVO;
import com.med.vo.DoctorMessageVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Project:internet-based-healthcare
 * Package:com.med.controller
 *
 * @Description:
 * @Author:其一
 * @CreateTime: 2025-08-25-23-59
 */
@RestController
@RequestMapping("/chat")
public class ChatController extends BaseController {
    @Autowired
    private IChatService chatService;

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @PostMapping("/session/create")
    public AjaxResult createSession(){
        return AjaxResult.success(chatService.createSession());
    }

    @PostMapping("/session/end")
    public R<Boolean> endSession(@RequestParam Integer sessionId) {
        return R.ok(chatService.endSession(sessionId));
    }

    @MessageMapping("/doctor/send")
    public void sendDoctorMessage(DoctorMessageDO doctorMessageDO) {
        // 1. 验证会话是否有效
        if (chatService.checkSessionValid(doctorMessageDO.getDoctorSessionId()) == null) {
            return;
        }

        // 2. 保存消息到数据库
        chatService.saveDoctorMessage(doctorMessageDO);

        // 3. 推送消息给接收者
        String pushDestination;
        if (doctorMessageDO.getDoctorMessageReceiverType() == 1) {
            // 接收者是医生
            pushDestination = "/doctor/msg/" + doctorMessageDO.getDoctorMessageReceiverId();
        } else {
            // 接收者是用户
            pushDestination = "/user/doctor/" + doctorMessageDO.getDoctorMessageReceiverId();
        }
        simpMessagingTemplate.convertAndSend(pushDestination, doctorMessageDO);
    }

    @GetMapping("/doctor/list")
    public TableDataInfo getDoctorMsgList(DoctorMessageVO doctorMessageVO) {
        startPage();
        List<DoctorMessageDO> doctorMsgPage = chatService.getDoctorMsgPage(doctorMessageVO);
        return getDataTable(doctorMsgPage);
    }


    @MessageMapping("/customer/send")
    public void sendCustomerMessage(CustomerMessageDO customerMessageDO) {
        // 1. 验证会话是否有效
        if (chatService.checkSessionValid(customerMessageDO.getCustomerSessionId()) == null) {
            return; // 无效会话不处理
        }

        // 2. 保存消息到数据库
        chatService.saveCustomerMessage(customerMessageDO);

        // 3. 推送消息给接收者
        String pushDestination;
        if (customerMessageDO.getCustomerMessageReceiverType() == 1) {
            // 接收者是客服
            pushDestination = "/customer/msg/" + customerMessageDO.getCustomerMessageReceiverId();
        } else {
            // 接收者是用户
            pushDestination = "/user/customer/" + customerMessageDO.getCustomerMessageReceiverId();
        }
        simpMessagingTemplate.convertAndSend(pushDestination, customerMessageDO);
    }

    @GetMapping("/customer/list")
    public TableDataInfo getCustomerMsgList(CustomerMessageVO customerMessageVO) {
        startPage();
        List<CustomerMessageDO> customerMsgPage = chatService.getCustomerMsgPage(customerMessageVO);
        return getDataTable(customerMsgPage);
    }

}
